package dao;

import logic.Pilot;
import org.hibernate.Criteria;
import org.hibernate.Session;
import util.HibernateUtil;

import javax.persistence.Query;
import javax.persistence.criteria.Expression;
import java.util.List;

public class PilotDAOImpl  {

    private Session sessionFactory;

    public void addPilot(Pilot pilot) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        sessionFactory.save(pilot);
        sessionFactory.getTransaction().commit();
        sessionFactory.close();
    }

    public List<Pilot> listPilot() {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        List<Pilot> pilots = sessionFactory.createCriteria(Pilot.class).list();
        sessionFactory.getTransaction().commit();
        sessionFactory.close();

        return pilots;
    }

    public List<Pilot> getPilotById(int pilotID) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        List<Pilot> pilot = sessionFactory.createSQLQuery("select * from Pilots where id = " + pilotID ).addEntity(Pilot.class).list();
        sessionFactory.getTransaction().commit();
        sessionFactory.close();

        return pilot;
    }

    public void removePilot(Integer id) {
        sessionFactory = HibernateUtil.getSessionFactory().openSession();
        sessionFactory.beginTransaction();
        Pilot pilot = (Pilot) sessionFactory.load(Pilot.class, id);
        if (pilot != null) {
            sessionFactory.delete(pilot);
        }
        sessionFactory.getTransaction().commit();
        sessionFactory.close();
    }
}
